import * as request from '../constant/fetch-request'
import HTTP_CODE from '../constant/http-code'
import { getReviewHomeworkQuiz } from './reviewQuiz'

export const refreshTask = data => ({
  type: 'REFRESH_TASK',
  data
})

export const refreshRunningLogs = runningLogs => ({
  type: 'REFRESH_RUNNING_LOGS',
  runningLogs
})

export const refreshHomeworkRenderDate = data => ({
  type: 'REFRESH_HOMEWORK_QUIZZES',
  data
})

export const refreshHomeworkQuizzes = data => ({
  type: 'REFRESH_HOMEWORK_QUIZZES',
  data
})
export const tutorFollowStudent = myStudent => ({
  type: 'FOLLOW_STUDENT_PROGRAM',
  myStudent
})

export const getTask = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/tasks/${taskId}`)

      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshTask(res.body))
      }
    })()
  }
}

export const submitAnswer = (data, loadLogs) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/v2/homeworkQuizzes/submission`, data)
      if (res.status === HTTP_CODE.CREATED) {
        loadLogs(res.body.id)
        dispatch(getReviewHomeworkQuiz(data.assignmentId, data.quizId))
        dispatch(getHomeworkQuizzes(data.assignmentId))
      }
    })()
  }
}

export const getHomeworkQuizzes = (assignmentId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/assignments/${assignmentId}/quizzes`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshHomeworkQuizzes(res.body))
      }
    })()
  }
}

export const getRunningLogs = (taskId, assignmentId, quizId, submissionId, getResult) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/homeworkQuizzes/submissions/${submissionId}/students/tasks/${taskId}/assignments/${assignmentId}/quizzes/${quizId}/runningLog`)
      if (res.status === HTTP_CODE.OK) {
        if (getResult) {
          getResult(res.body)
        } else {
          dispatch(refreshRunningLogs(res.body))
        }
      } else {
        dispatch(refreshRunningLogs({}))
      }
    })()
  }
}

export const getStudentHomeworkQuizzes = (assignmentId, studentId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/assignments/${assignmentId}/students/${studentId}/quizzes`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshHomeworkRenderDate(res.body))
      }
    })()
  }
}

export const getTutorFollowStudent = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/myStudents/programs/${programId}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(tutorFollowStudent(res.body))
      }
    })()
  }
}
export const submitQuizSuggestion = (suggestion,callback) => {
  return () => {
    (async () => {
      const res = await request.post(`../api/v2/quizzes/suggestions`,suggestion)
      if (res.status === HTTP_CODE.CREATED) {
        callback()
      }
    })()
  }
}
export const getAssignmentDuration = (taskId,assignmentId,callback) => {
  return () => {
    (async () => {
      const res = await request.get(`../api/v2/tasks/${taskId}/assignments/${assignmentId}`)
      if (res.status === HTTP_CODE.OK) {
        callback(res.body)
      }
    })()
  }
}
export const startAssignment = (userId,taskId,assignmentId) => {
  return () => {
    (async () => {
      await request.post(`../api/v2/tasks/assignments/users`,{userId,taskId,assignmentId})
    })()
  }
}
